package io.itit.ecp.admin.provide.action.resp;


import cn.hutool.core.util.ObjectUtil;
import io.itit.grass.common.provide.action.resp.BaseResp;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.Objects;

@Data
@ApiModel("规实分析响应结果")
@EqualsAndHashCode(callSuper = true)
public class PracticalAnalysisResp extends BaseResp {

    private String planId;

    /* 项目Id */
    private String sceneProjectId;

    @ApiModelProperty("项目名称")
    private String projectName;

    /* 数据日期 */
    @ApiModelProperty("数据日期")
    private LocalDate solidifyDate;

    /*规划碳排 */
    @ApiModelProperty("规划碳排")
    private BigDecimal planCarbonEmissions;

    @ApiModelProperty("实际碳排")
    private BigDecimal actualCarbonEmissions;

    /*规划碳减 */
    @ApiModelProperty("规划碳减")
    private BigDecimal planCarbonZero;

    @ApiModelProperty("实际碳减")
    private BigDecimal actualCarbonZero;

    /*规划碳汇*/
    @ApiModelProperty("规划碳汇")
    private BigDecimal planCarbonSequestration;

    @ApiModelProperty("实际碳汇")
    private BigDecimal actualCarbonSequestration;

    /* 规划碳中和 */
    @ApiModelProperty("规划碳中和")
    private BigDecimal planCarbonNeutrality;

    @ApiModelProperty("实际碳中和")
    private BigDecimal actualCarbonNeutrality;

    @ApiModelProperty("碳排偏差值")
    public BigDecimal getCarbonEmissionsDeviation() {
        if (Objects.isNull(planCarbonEmissions) || Objects.isNull(actualCarbonEmissions)) {
            return null;
        }
        return planCarbonEmissions.subtract(actualCarbonEmissions);
    }

    @ApiModelProperty("碳减偏差值")
    public BigDecimal getCarbonZeroDeviation() {
        if (Objects.isNull(planCarbonZero) || Objects.isNull(actualCarbonZero)) {
            return null;
        }
        return planCarbonZero.subtract(actualCarbonZero);
    }

    @ApiModelProperty("碳汇偏差值")
    public BigDecimal getCarbonSequestrationDeviation() {
        if (Objects.isNull(planCarbonSequestration) || Objects.isNull(actualCarbonSequestration)) {
            return null;
        }
        return planCarbonSequestration.subtract(actualCarbonSequestration);
    }

    @ApiModelProperty("碳中和偏差值")
    public BigDecimal getCarbonNeutralityDeviation() {
        if (Objects.isNull(planCarbonNeutrality) || Objects.isNull(actualCarbonNeutrality)) {
            return null;
        }
        return planCarbonNeutrality.subtract(actualCarbonNeutrality).setScale(2, RoundingMode.HALF_UP);
    }

    @ApiModelProperty("碳排偏差贡献率")
    public BigDecimal getCarbonEmissionsDeviationRate() {
        BigDecimal carbonEmissionsDeviation = getCarbonEmissionsDeviation();
        BigDecimal zeroDeviation = getCarbonZeroDeviation();
        BigDecimal carbonSequestrationDeviation = getCarbonSequestrationDeviation();
        BigDecimal sumDeviation = ObjectUtil.defaultIfNull(carbonEmissionsDeviation, BigDecimal.ZERO)
                .add(ObjectUtil.defaultIfNull(zeroDeviation, BigDecimal.ZERO))
                .add(ObjectUtil.defaultIfNull(carbonSequestrationDeviation, BigDecimal.ZERO));
        if (sumDeviation.doubleValue() == 0.0D) {
            return null;
        }
        return carbonEmissionsDeviation.divide(sumDeviation, RoundingMode.HALF_DOWN).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP);
    }

    @ApiModelProperty("碳减偏差贡献率")
    public BigDecimal getCarbonZeroDeviationRate() {
        BigDecimal carbonEmissionsDeviation = getCarbonEmissionsDeviation();
        BigDecimal zeroDeviation = getCarbonZeroDeviation();
        BigDecimal carbonSequestrationDeviation = getCarbonSequestrationDeviation();
        BigDecimal sumDeviation = ObjectUtil.defaultIfNull(carbonEmissionsDeviation, BigDecimal.ZERO)
                .add(ObjectUtil.defaultIfNull(zeroDeviation, BigDecimal.ZERO))
                .add(ObjectUtil.defaultIfNull(carbonSequestrationDeviation, BigDecimal.ZERO));
        if (sumDeviation.doubleValue() == 0.0D) {
            return null;
        }
        return zeroDeviation.divide(sumDeviation, RoundingMode.HALF_DOWN).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP);
    }

    @ApiModelProperty("碳汇偏差贡献率")
    public BigDecimal getCarbonSequestrationDeviationRate() {
        BigDecimal carbonEmissionsDeviation = getCarbonEmissionsDeviation();
        BigDecimal zeroDeviation = getCarbonZeroDeviation();
        BigDecimal carbonSequestrationDeviation = getCarbonSequestrationDeviation();
        BigDecimal sumDeviation = ObjectUtil.defaultIfNull(carbonEmissionsDeviation, BigDecimal.ZERO)
                .add(ObjectUtil.defaultIfNull(zeroDeviation, BigDecimal.ZERO))
                .add(ObjectUtil.defaultIfNull(carbonSequestrationDeviation, BigDecimal.ZERO));
        if (sumDeviation.doubleValue() == 0.0D) {
            return null;
        }
        return carbonSequestrationDeviation.divide(sumDeviation, RoundingMode.HALF_DOWN).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP);
    }
}
